package com.forexample.myapplication

import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.forexample.myapplication.databinding.FragmentFirstBinding
import java.text.SimpleDateFormat

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class FirstFragment : Fragment() {

    private var _binding: FragmentFirstBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!
    private val myHandler:MyHandler= MyHandler()
    private var timeFormat:SimpleDateFormat?=null;
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentFirstBinding.inflate(inflater, container, false)
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        timeFormat= SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ssss")
        myHandler.sendEmptyMessage(1)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }



   inner class MyHandler : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what==1){
                binding.textviewFirst.text=timeFormat!!.format(System.currentTimeMillis())
                myHandler.sendEmptyMessageDelayed(1,1000)

            }
        }
    }
}